package com.example.xiaoxueqixiangmu.jiemian;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.xiaoxueqixiangmu.LoginActivity;
import com.example.xiaoxueqixiangmu.R;
import com.example.xiaoxueqixiangmu.utils.Constant;
import com.example.xiaoxueqixiangmu.utils.NetUtils;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class editPasswordActivity extends AppCompatActivity {
    private EditText passWord1,passWord2;
    private Button btnSubmit;
    private ProgressDialog progressDialog;
    private  String pwd1,pwd2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);
        passWord1=(EditText) findViewById(R.id.newpwd);
        passWord2=(EditText)findViewById(R.id.confirmpwd);
        btnSubmit=(Button)findViewById(R.id.btnSubmit);



        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwd1=passWord1.getText().toString().trim();
                pwd2=passWord2.getText().toString().trim();
                if (pwd1.equals(pwd2)){
                    if (!NetUtils.check(editPasswordActivity.this)){
                        Toast.makeText(editPasswordActivity.this,"网络异常！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //弹出对话框
                    progressDialog=ProgressDialog.show(editPasswordActivity.this,null,"正在加载中...",false,true);
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            SharedPreferences preferences=editPasswordActivity.this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                            String value=preferences.getString("cookie","");
                            try {
                                OkHttpClient client=new OkHttpClient();
                                RequestBody requestBody=new FormBody.Builder()
                                        .add("newPassword",pwd1)
                                        .add("action","update")
                                        .build();
                                Request request=new Request.Builder()
                                        .addHeader("Cookie",value)
                                        .url(Constant.Host+"/otn/AccountPassword")
                                        .post(requestBody)
                                        .build();
                                Response response=client.newCall(request).execute();
                                String result=response.body().string();
                                if ("\"1\"".equals(result)) {
                                    if (progressDialog != null) {
                                        progressDialog.dismiss();
                                    }
                                }
                            }catch (IOException e){
                                e.printStackTrace();
                            }
                        }
                    }.start();
                    Intent intent=new Intent(editPasswordActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    new AlertDialog.Builder(editPasswordActivity.this)
                            .setIcon(R.drawable.hint)
                            .setTitle("注意")
                            .setMessage("密码不一致，请再次确认！")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            })
                            .setNegativeButton("取消",null)
                            .create()
                            .show();
                }
            }
        });
    }
}
